package com.sailfish.springbootdemo.controller.db2;

import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db2.PcbCapacityParam;
import com.sailfish.springbootdemo.pojo.db2.PcbSonFactoryInfo;
import com.sailfish.springbootdemo.service.db2.PcbSonFactoryInfoService;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
@CrossOrigin
@RequestMapping("/pcbSonFactoryInfo")
public class PcbSonFactoryInfoController {

    @Autowired
    private PcbSonFactoryInfoService pcbSonFactoryInfoService;

    @PostMapping("/add")
    public Result insert(Integer parentId, String sonFactoryName, String sonFactoryNameEn, String companyCode,
                         String sonFactoryIntroduction, String sonFactoryIntroductionEn, String sonFactoryContacts, String sonFactoryPhone,
                         String sonFactoryEmail, String sonFactoryCategory, String sonFactoryCategoryEn, Integer status, Integer sort) {
        //判断一些必填项
        if (parentId == null) {
            return ResultUtil.error(500, "缺少参数parentId", "Miss Param parentId", null);
        }
        if (StringUtils.isBlank(sonFactoryName) && StringUtils.isBlank(sonFactoryNameEn)) {
            return ResultUtil.error(500, "缺少参数sonFactoryName", "Miss Param sonFactoryName", null);
        }
        if (StringUtils.isBlank(sonFactoryCategory) && StringUtils.isBlank(sonFactoryCategoryEn)) {
            return ResultUtil.error(500, "缺少参数sonFactoryCategory", "Miss Param sonFactoryCategory", null);
        }
        try {
            status = status == null ? 1 : status;
            PcbSonFactoryInfo pcbSonFactoryInfo = new PcbSonFactoryInfo();
            pcbSonFactoryInfo.setParentId(parentId);
            pcbSonFactoryInfo.setSonFactoryName(sonFactoryName);
            pcbSonFactoryInfo.setSonFactoryNameEn(sonFactoryNameEn);
            pcbSonFactoryInfo.setCompanyCode(companyCode);
            pcbSonFactoryInfo.setSonFactoryIntroduction(sonFactoryIntroduction);
            pcbSonFactoryInfo.setSonFactoryIntroductionEn(sonFactoryIntroductionEn);
            pcbSonFactoryInfo.setSonFactoryContacts(sonFactoryContacts);
            pcbSonFactoryInfo.setSonFactoryPhone(sonFactoryPhone);
            pcbSonFactoryInfo.setSonFactoryEmail(sonFactoryEmail);
            pcbSonFactoryInfo.setSonFactoryCategory(sonFactoryCategory);
            pcbSonFactoryInfo.setSonFactoryCategoryEn(sonFactoryCategoryEn);
            java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
            pcbSonFactoryInfo.setCreateTime(date);
            pcbSonFactoryInfo.setStatus(status);
            pcbSonFactoryInfo.setSort(sort);
            return pcbSonFactoryInfoService.insert(pcbSonFactoryInfo);
        } catch (Exception e) {
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @PostMapping("/getAll")
    public Result getAll(String searchK,String searchV) {
        String companyName = null;
        String companyNameEn = null;
        String companyWebsite = null;
        String sonFactoryName = null;
        String sonFactoryNameEn = null;
        String sonFactoryIntroduction = null;
        String sonFactoryContacts = null;
        String sonFactoryPhone = null;
        String sonFactoryEmail = null;
        String sonFactoryCategory = null;
        Integer status = null;
        if (!StringUtils.isBlank(searchK)) {
            if (searchK.equals("companyName")) {
                companyName = searchV;
            }
            if (searchK.equals("companyNameEn")) {
                companyNameEn = searchV;
            }
            if (searchK.equals("companyWebsite")) {
                companyWebsite = searchV;
            }
            if (searchK.equals("sonFactoryNameEn")) {
                sonFactoryNameEn = searchV;
            }
            if (searchK.equals("sonFactoryName")) {
                sonFactoryName = searchV;
            }
            if (searchK.equals("sonFactoryIntroduction")) {
                sonFactoryIntroduction = searchV;
            }
            if (searchK.equals("sonFactoryContacts")) {
                sonFactoryContacts = searchV;
            }
            if (searchK.equals("sonFactoryPhone")) {
                sonFactoryPhone = searchV;
            }
            if (searchK.equals("sonFactoryEmail")) {
                sonFactoryEmail = searchV;
            }
            if (searchK.equals("sonFactoryCategory")) {
                sonFactoryCategory = searchV;
            }
            if (searchK.equals("status")) {
                status = Integer.parseInt(searchV) ;
            }
        }
        List<PcbSonFactoryInfo> children = pcbSonFactoryInfoService.getAll(null, companyName, companyNameEn, companyWebsite, sonFactoryName, sonFactoryNameEn, sonFactoryIntroduction,
                sonFactoryPhone, sonFactoryContacts, sonFactoryEmail, sonFactoryCategory, status);
        return ResultUtil.success(children);
    }


    @PostMapping("/getById")
    public Result getById(Integer id) {
        PcbSonFactoryInfo sonFactoryInfo = pcbSonFactoryInfoService.getById(id);
        return ResultUtil.success(sonFactoryInfo);
    }

    @PostMapping("/getByIds")
    public Result getByIds(@RequestBody List<Integer> ids) {
        if (ids == null) {
            return ResultUtil.error(500, "参数ids为空", "param ids miss", null);
        }
        List<PcbSonFactoryInfo> factoryInfos = pcbSonFactoryInfoService.getByIds(ids);
        return ResultUtil.success(factoryInfos);
    }

    @PostMapping("/delete")
    public Result deleteById(Integer id) {
        return pcbSonFactoryInfoService.delete(id);
    }

    @PostMapping("/update")
    public Result update(@RequestBody String data) {
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer id = jsonObject.getInteger("pcbSonFactoryId");
        Integer parentId = jsonObject.getInteger("parentId");
        String companyName = jsonObject.getString("companyName");
        String companyCode = jsonObject.getString("companyCode");
        String companyNameEn = jsonObject.getString("companyNameEn");
        String companyWebsite = jsonObject.getString("companyWebsite");
        String sonFactoryName = jsonObject.getString("sonFactoryName");
        String sonFactoryNameEn = jsonObject.getString("sonFactoryNameEn");
        String sonFactoryIntroduction = jsonObject.getString("sonFactoryIntroduction");
        String sonFactoryIntroductionEn = jsonObject.getString("sonFactoryIntroductionEn");
        String sonFactoryContacts = jsonObject.getString("sonFactoryContacts");
        String sonFactoryPhone = jsonObject.getString("sonFactoryPhone");
        String sonFactoryEmail = jsonObject.getString("sonFactoryEmail");
        String sonFactoryCategory = jsonObject.getString("sonFactoryCategory");
        String sonFactoryCategoryEn = jsonObject.getString("sonFactoryCategoryEn");
        Date createTime = jsonObject.getDate("createTime");
        Integer status = jsonObject.getInteger("status");
        Integer sort = jsonObject.getInteger("sort");
        if (id == null) {
            return ResultUtil.error(500, "参数id为空", "id is null", null);
        }
        PcbSonFactoryInfo item = new PcbSonFactoryInfo();
        item.setPcbSonFactoryId(id);
        item.setParentId(parentId);
        item.setCompanyName(companyName);
        item.setCompanyCode(companyCode);
        item.setCompanyNameEn(companyNameEn);
        item.setCompanyWebsite(companyWebsite);
        item.setSonFactoryName(sonFactoryName);
        item.setSonFactoryNameEn(sonFactoryNameEn);
        item.setSonFactoryIntroduction(sonFactoryIntroduction);
        item.setSonFactoryIntroductionEn(sonFactoryIntroductionEn);
        item.setSonFactoryContacts(sonFactoryContacts);
        item.setSonFactoryPhone(sonFactoryPhone);
        item.setSonFactoryEmail(sonFactoryEmail);
        item.setSonFactoryCategory(sonFactoryCategory);
        item.setSonFactoryCategoryEn(sonFactoryCategoryEn);
        item.setCreateTime(createTime);
        item.setStatus(status);
        item.setSort(sort);
        java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
        item.setUpdateTime(date);
        return pcbSonFactoryInfoService.update(item);
    }

}
